<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use app\model\Admin as AD;
use app\model\Role as RO;
use app\model\Admin_Role as AR;
use app\model\Node as ND;
use think\facade\Session;


class Admin extends BaseController
{
    public function LoginOut(){
        Session::delete('admin');
        return redirect("/index/index");
    }

    protected $middleware = [
        // 对所有方法有效
        '\app\middleware\NavCheck',
        '\app\middleware\UrlCheck',
        '\app\middleware\LoginCheck',
    ];
    public function index()
    {
        return view('index');
    }
    public function data(){
        $user = new AD;
        $limit = $this->request->param('limit');
        $page = $this->request->get('page');
        $offset = ($page-1)*$limit;
        $id= $this->request->param('id');
        $admin_name= $this->request->param('admin_name');
        $where = [];
        if(!empty($id)){
           $where[] = ['id','=',$id];
        }
        if(!empty($admin_name)){
            $where[] = ['admin_name','like',"%$admin_name%"];
        }

        $list = $user->where($where)
            //->leftJoin('admin_role ar',"admin.id=ar.admin_id")
            //->leftJoin('role r',"ar.role_id=r.id")
            ->limit($offset,$limit)
            ->field('admin.*')
            ->select();
       foreach ($list as $k =>$v){
           $role_list = RO::
           leftJoin('admin_role ar',"role.id=ar.role_id")
               ->where('admin_id',$v['id'])
               ->column('role_name');
           //var_dump($list[$k]);
           $list[$k]['role_name'] = implode(",",$role_list);

       }
        $count = AD::count();
        return json(["code"=>0,"msg"=>"查询成功","count"=>$count,"data"=>$list]);
    }

    public function add(){
        if($this->request->isPost()){
            $data = $this->request->post();

            if(isset($data['role_id'])){
                $role_id = $data['role_id'];
                unset($data['role_id']);
            }

            $data['admin_pwd']=md5($data['admin_pwd']);
            $user = new AD();
            $res = $user->save($data);
            $res2 = true;

            if(!empty($role_id)){
                $admin_id = $user->id;
                $ar = [];
                foreach ($role_id as $key=>$value){
                    $ar[] = ["admin_id"=>$admin_id,"role_id"=>$value];
                }
                $AR = new AR();
                $res2 = $AR->saveAll($ar);
            }

            if($res){
                if($res2){
                    $this->success("添加成功",'/Admin/index');
                }
            }else{
                $this->error("添加失败");
            }

        }
        $list = RO::select();
        return view('add',['list'=>$list]);
    }

    public function del(){  //删除方法
        $id=$this->request->post('id');
        $res = AD::destroy($id);
        $res2 = AR::where('admin_id',$id)->delete();
        if($res){
            if($res2){
                return json(["code"=>0,"msg"=>"删除成功"]);
            }
        }else{
            return json(["code"=>400,"msg"=>"删除失败"]);
        }
    }

    public function update(){
        if($this->request->isPost()){
            $data = $this->request->post();

            AR::where('admin_id',$data['id'])->delete();
            $res2 = true;

            if(isset($data['role_id'])){
                $role_id = [];
                foreach ($data['role_id'] as $k=>$v){
                    $role_id[] = ["admin_id"=>$data['id'],"role_id"=>$v];
                }
                $user1 = new AR();
                $res2 = $user1->saveAll($role_id);
                unset($data['role_id']);
            }

            $pwd = AD::find($data['id']);
            if(empty($data['admin_pwd'])){
                unset($data['pwd']);
            }else if($data['admin_pwd']==$pwd['admin_pwd']){
                $data['admin_pwd']=$pwd['admin_pwd'];
            }
            $data['admin_pwd'] = md5($data['admin_pwd']);

            $user = AD::find($data['id']);
            $res =  $user->save($data);
            if($res){
                if($res2){
                    $this->success('修改成功','index');
                }
            }else{
                $this->error('修改失败');
            }

        }

        $id= $this->request->param('id');
        $user = AD::find($id);

        //$a_r = AR::where("admin_id",$user['id'])->field("role_id")->select();
        $a_r = AR::where("admin_id",$user['id'])->column('role_id');
        //var_dump($a_r->toArray());die();
        $list = RO::select();

        return view('add',['admin'=>$user,"role"=>$a_r,'list'=>$list]);
    }


}
